﻿using SharpML.Api.Implementation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharpML.Api.Types
{
    public class Layer
    {
        public bool UseBias { get; set; }
        public int Size { get; private set; }
        public float[] Values { get; set; }

        public Layer( int size, bool useBias = true )
            : this( size, new float[size] )
        {
            UseBias = useBias;
        }

        public Layer( int size, params float[] values )
        {
            Size = size;
            Values = values;
            SetBias();
            if( values.Count() != size )
                throw new ArgumentException( "values" );
        }


        public Layer Clone()
        {
            Layer layer = new Layer( Size );
            Array.Copy( Values, layer.Values, Size );
            return layer;
        }

        public void ClearValues()
        {
            SetValues(0);

            if( UseBias )
                SetBias();
        }

        public void SetBias( float value = 1.01f )
        {
            // First node is always the bias node!
            Values[0] = value;
        }

        public void SetValues( float value )
        {
            for( int i = 0; i < Size; i++ )
                Values[i] = value;

            if( UseBias )
                SetBias();
        }

        public void CopyBinary( Layer input )
        {
            for( int index = 0; index < Size; index++ )
                Values[index] = Randomized.Default.NextDouble() <= Values[index] ? 1.0f : 0.0f;

            if( UseBias )
                SetBias();
        }
    }
}
